//
//  MainTabBarController.swift
//  flowerpot
//
//  Created by fyname on 15/11/4.
//  Copyright © 2015年 senhe. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

    var itemNameArray:[String] = ["index48","log48","qz48","qa48","mall48"]
    var itemNameSelectArray:[String] = ["index48","log48","qz48","qa48","mall48"]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        configTabBar()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func configTabBar() {
        var count:Int = 0;
        let items = self.tabBar.items
        for item in items! as [UITabBarItem] {
            var image:UIImage = UIImage(named: itemNameArray[count])!
            var selectedimage:UIImage = UIImage(named: itemNameSelectArray[count])!;
            

            image = image.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);

            selectedimage = selectedimage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
            item.selectedImage = selectedimage;
            item.image = image;
            count++;
        }
        
        //显示有新未读信息标示
        /*let num:Int = 5
        
        if(num > 0){
            
            self.tabBarItem.badgeValue = "\(num)"
            
        }else{
            
            self.tabBarItem.badgeValue = nil
        }*/
        
    }
    
    
    // 覆写了 TabBar 的点击效果
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        
        print("item:\(item.title)")
        
        if(item.title == "首页"){
            print("index")
            Common.rootViewController.mainTabBarController.view.addSubview(Common.indexVC.view)
            Common.rootViewController.mainTabBarController.view.bringSubviewToFront(Common.rootViewController.mainTabBarController.tabBar)
            Common.rootViewController.showHome()
        }else if(item.title == "日志"){
            print("log")
            Common.rootViewController.mainTabBarController.view.addSubview(Common.logVC.view)
            Common.rootViewController.mainTabBarController.view.bringSubviewToFront(Common.rootViewController.mainTabBarController.tabBar)
            Common.rootViewController.showHome()
        }else if(item.title == "圈子"){
            print("qz")
            Common.rootViewController.mainTabBarController.view.addSubview(Common.qzVC.view)
            Common.rootViewController.mainTabBarController.view.bringSubviewToFront(Common.rootViewController.mainTabBarController.tabBar)
            Common.rootViewController.showHome()
        }else if(item.title == "问答"){
            print("qa")
            Common.rootViewController.mainTabBarController.view.addSubview(Common.qaVC.view)
            Common.rootViewController.mainTabBarController.view.bringSubviewToFront(Common.rootViewController.mainTabBarController.tabBar)
            Common.rootViewController.showHome()
        }else if(item.title == "商城"){
            print("mall")
            Common.rootViewController.mainTabBarController.view.addSubview(Common.mallVC.view)
            Common.rootViewController.mainTabBarController.view.bringSubviewToFront(Common.rootViewController.mainTabBarController.tabBar)
            Common.rootViewController.showHome()
        }else{
            print("unknow")
        }
            
 }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
